import { _decorator, Component, Node, Label, director, tween, UITransform, UIOpacity, Tween } from 'cc';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import AdManager from 'db://assets/Script/Manager/AdManager';
import SSS_GameEventManager, { SS_GameEventData } from 'db://assets/Script/Manager/SSS_GameEventManager';
import { SSS_Consts } from 'db://assets/Script/Data/SSS_Consts';
import { DaoShuiPaiXu_GameStorage } from '../Data/DaoShuiPaiXu_GameStorage';
import DaoShuiPaiXu_GameManager from '../DaoShuiPaiXu_GameManager';
import { DaoShuiPaiXu_GameData } from '../Data/DaoShuiPaiXu_GameData';
import { CupMgr } from '../views/cupMgr';
import { Global } from '../Global';
import SSS_ShuaiFunction from 'db://assets/Script/Utils/SSS_ShuaiFunction';
import { SSS_Lockable } from 'db://assets/Script/Common/SSS_Lockable';

const { ccclass, property } = _decorator;

@ccclass('DaoShuiPaiXu_GamePanel')
export default class DaoShuiPaiXu_GamePanel extends Component {
    static Instance: DaoShuiPaiXu_GamePanel;

    @property(Label)
    public Label_level: Label | null = null;

    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;

    @property(Node)
    Button_撤回: Node | null = null;
    @property(Node)
    Button_加瓶子: Node | null = null;

    @property(Label)
    Label_撤回_number: Label | null = null;
    @property(Node)
    撤回广告icon: Node | null = null;

    @property(Label)
    Label_加瓶子_number: Label | null = null;
    @property(Node)
    加瓶子广告icon: Node | null = null;

    @property(Label)
    public Label_数量上限提示: Label | null = null;

    onLoad() {
        DaoShuiPaiXu_GamePanel.Instance = this;
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        this.Label_level.string = "第 " + DaoShuiPaiXu_GameData.Get_level() + " 关";

        // this.Label_撤回_number.string = `${DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number}`;
        this.Label_加瓶子_number.string = `${DaoShuiPaiXu_GameData.Get_item_加瓶子_number()}`;

        // this.撤回广告icon.active = DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number <= 0;
        this.加瓶子广告icon.active = DaoShuiPaiXu_GameData.Get_item_加瓶子_number() <= 0;
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

        this.Button_撤回.on(Node.EventType.TOUCH_END, this.On_Button_撤回, this);
        this.Button_加瓶子.on(Node.EventType.TOUCH_END, this.On_Button_加瓶子, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

        this.Button_撤回.off(Node.EventType.TOUCH_END, this.On_Button_撤回, this);
        this.Button_加瓶子.off(Node.EventType.TOUCH_END, this.On_Button_加瓶子, this);
    }

    On_Button_撤回() {
        DaoShuiPaiXu_GameManager.Instance.撤回();

        // if (DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number > 0) {
        //     DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number--;
        //     DaoShuiPaiXu_GameStorage.saveData();
        //     this.Refresh();

        //     DaoShuiPaiXu_GameManager.Instance.撤回();
        // }
        // else {
        //     AdManager.是否看完广告 = false;
        //     AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
        //         () => {
        //             director.emit(SS_GameEventData.广告结束);

        //             this.增加道具撤回次数();
        //         },
        //         () => {
        //             director.emit(SS_GameEventData.广告结束);

        //             console.log("广告没看完！");
        //         },
        //     );
        // }

        SSS_AudioManager.PlayButtonAudio();
    }

    @SSS_Lockable(1000)
    On_Button_加瓶子() {
        if (CupMgr.ins.curCfg.length >= Global.maxTube) {

            this.Label_数量上限提示.node.active = true;
            let opacity = this.Label_数量上限提示.node.getComponent(UIOpacity);
            opacity.opacity = 255;

            Tween.stopAllByTarget(opacity);
            tween(opacity)
                .delay(1)
                .to(1, { opacity: 0 })
                .call(() => {
                    this.Label_数量上限提示.node.active = false;
                    opacity.opacity = 0;
                })
                .start();
        }

        if (!CupMgr.ins.checkCanAddTube()) return;

        if (DaoShuiPaiXu_GameData.Get_item_加瓶子_number() > 0) {
            DaoShuiPaiXu_GameData.Set_item_加瓶子_number(DaoShuiPaiXu_GameData.Get_item_加瓶子_number() - 1);
            this.Refresh();

            DaoShuiPaiXu_GameManager.Instance.加瓶子();
        }
        else {
            SSS_ShuaiFunction.AD_callback(() => {
                this.增加道具加瓶子次数();
            });


            // AdManager.是否看完广告 = false;
            // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         this.增加道具加瓶子次数();
            //     },
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         console.log("广告没看完！");
            //     },
            // );
        }

        SSS_AudioManager.PlayButtonAudio();
    }

    增加道具撤回次数() {
        // DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number += DaoShuiPaiXu_GameData.item_撤回_add_number;
        // DaoShuiPaiXu_GameStorage.saveData();
        // this.Refresh();
    }
    增加道具加瓶子次数() {
        DaoShuiPaiXu_GameData.Set_item_加瓶子_number(DaoShuiPaiXu_GameData.Get_item_加瓶子_number() + DaoShuiPaiXu_GameData.item_加瓶子_add_number);
        this.Refresh();
    }

    On_Button_back() {
        // SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Menu);
        director.loadScene(SSS_Consts.SceneName.DaoShuiPaiXu_Main_Scene);

        SSS_AudioManager.PlayButtonAudio();
    }

    On_Button_refresh() {
        director.loadScene(SSS_Consts.SceneName.DaoShuiPaiXu_Game_Scene);

        SSS_AudioManager.PlayButtonAudio();
    }
}

// /** 调试 */
window["DaoShuiPaiXu_GamePanel"] = DaoShuiPaiXu_GamePanel;